﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SStuff.FlowControls
{
    [TemplateVisualState(GroupName = "SelectionStates", Name = "Unselected")]
    [TemplateVisualState(GroupName = "SelectionStates", Name = "Selected")]
    [TemplateVisualState(GroupName = "MouseStates", Name = "MouseOver")]
    [TemplateVisualState(GroupName = "MouseStates", Name = "MouseOut")]
    public class FlowItemBase : ContentControl
    {

        public double RelativeZIndex { get; set; }

        public FlowItemsControlBase Owner
        {
            get { return (FlowItemsControlBase)GetValue(OwnerProperty); }
            set { SetValue(OwnerProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Owner.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty OwnerProperty =
            DependencyProperty.Register("Owner", typeof(FlowItemsControlBase), typeof(FlowItemBase), new PropertyMetadata(null));

        bool _templateApplied = false;
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            UpdateSelectionVisualState();
            _templateApplied = true;
        }

        private void UpdateSelectionVisualState()
        {
            VisualStateManager.GoToState(this, IsSelected ? "Selected" : "Unselected", _templateApplied);
        }
        public bool IsSelected
        {
            get { return (bool)GetValue(IsSelectedProperty); }
            set { SetValue(IsSelectedProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsSelected.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsSelectedProperty =
            DependencyProperty.Register("IsSelected", typeof(bool), typeof(FlowItemBase), new PropertyMetadata(false, (s, a) =>
                {
                    var asItem = s as FlowItemBase;
                    if (asItem.IsSelected && asItem.Owner != null)
                        asItem.Owner.SelectedItem = asItem.Content;
                    asItem.UpdateSelectionVisualState();
                }));


        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            CaptureMouse();
            IsSelected = true;
        }
        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonUp(e);
            ReleaseMouseCapture();
        }

        protected override void OnMouseEnter(MouseEventArgs e)
        {
            base.OnMouseEnter(e);
            if (this.IsInMainVisualTree())
                VisualStateManager.GoToState(this, "MouseOver", true);
        }
        protected override void OnMouseLeave(MouseEventArgs e)
        {
            base.OnMouseLeave(e);
            if (this.IsInMainVisualTree())
                VisualStateManager.GoToState(this, "MouseOut", true);
        }
        

    }
}
